package com.leyou.auth.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.leyou.auth.entity.ClientInfo;
import com.leyou.auth.mapper.ClientMapper;
import com.leyou.auth.service.ClientService;
import com.leyou.common.exception.LyException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

@Service
public class ClientServiceImpl extends ServiceImpl<ClientMapper,ClientInfo> implements ClientService {
    @Autowired
    PasswordEncoder passwordEncoder;
    @Value("${ly.jwt.key}")
    private String key;
    @Override
    public String getSecretKey(String clientId, String secret) {
        // 1.查询client信息
        ClientInfo clientId1 = query().eq("client_id", clientId).one();
        //健壮性判断
        if (clientId1==null){
            throw new LyException(401,"客户端有误查询不到");
        }
        // 2.校验client的secret
        //比较数据库与参数中秘钥
        if (!passwordEncoder.matches(secret,clientId1.getSecret())){
            //不相等说明有误
            throw new LyException(401, "客户端的信息有误，secret不正确！");
        }
        //相等 返回配置文件中的秘钥
        return key;
    }
}
